public class SingleLinked {

    class LinkedNode {
        public LinkedNode next;
        public Integer value;

        public LinkedNode(int value) {
            this.value = value;
        }
    }

    LinkedNode head = null;

    /**
     * addFirst 首插法
     *
     * @param value int类型的值
     */
    public void addFirst(int value) {
        LinkedNode node = new LinkedNode(value);
        if (head == null) {
            head = node;
        } else {
            node.next = head;
            head = node;
        }
    }

    /**
     * addLast 尾插法
     *
     * @param value int类型的值
     */
    public void addLast(int value) {
        LinkedNode node = new LinkedNode(value);
        if (head == null) {
            head = node;
        } else {
            LinkedNode cur = head;
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = node;
        }
    }

    /**
     * 任意位置插入data节点
     * @param index 插入位置
     * @param data 插入节点的数据
     */
    public void addIndex(int index, int data) {
        LinkedNode node = new LinkedNode(data);
        //index不合法
        if (index < 0 || index > size()) {
            throw new IndexOutOfBoundsException("index不合法：index = " + index);
        }
        //如果从头插入，直接头插法
        if (index == 0) {
            addFirst(data);
        }
        //从中间插入
        LinkedNode prve = searchPrve(index);
        node.next = prve.next;
        prve.next = node;
    }

    private LinkedNode searchPrve(int index) {
        LinkedNode cur = head;
        int ret = 0;
        while (ret < index - 1) {
            cur = cur.next;
            ret++;
        }
        return cur;
    }

    /**
     * 查找是否包含关键字key是否在单链表当中
     * @param key 关键字
     * @return
     */
    public boolean contains(int key) {
        LinkedNode cur = head;
        while(cur != null) {
            if(cur.value != key) {
                cur = cur.next;
            } else {
                return true;
            }
        }
        return false;
    }

    /**
     * 删除第一次出现关键字为key的节点
     * @param key 删除key节点
     */
    public void remove(int key) {
        //如果key是头节点
        if(head.value == key) {
            head = head.next;
            return;
        }
        //寻找key的前面一个节点
        LinkedNode prve = searchRemovePrve(key);
        //删除key节点
        LinkedNode prveNext = prve.next;
        prve.next = prveNext.next;
    }
    private LinkedNode searchRemovePrve(int key) {
        LinkedNode cur = head;
        while(cur != null) {
            if(cur.next.value == key) {
                return cur;
            } else {
                cur = cur.next;
            }
        }
        return null;
    }

    /**
     * 删除所有值为key的节点
     * @param key
     */
    public void removeAllKey(int key) {
        LinkedNode prve = head;
        LinkedNode cur = head.next;
        while(cur != null) {
            if(cur.value == key) {
                prve.next = cur.next;
                cur = cur.next;
            } else {
                prve = cur;
                cur = cur.next;
            }
        }
        //删除头节点
        if(head.value == key) {
            head =head.next;
        }
    }

    /**
     * 获取到单链表的长度
     * @return
     */
    public int size() {
        LinkedNode cur = head;
        int count = 0;
        while (cur != null) {
            cur = cur.next;
            count++;
        }
        return count;
    }

    /**
     * 清除链表中的所有节点
     */
    public void clear() {
        head = null;
    }
}
